package com.deeper.customview.ui.c4.demo.c04d09;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.deeper.customview.R;

public class PorterDuffXferView extends View {
    public PorterDuffXferView(Context context) {
        super(context);
    }


    public PorterDuffXferView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }


    public PorterDuffXferView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Bitmap dstBitmap = Bitmap.createBitmap(160, 160, Bitmap.Config.ARGB_8888);
        Bitmap srcBitmap = dstBitmap.copy(Bitmap.Config.ARGB_8888, true);
        Canvas dstCanvas = new Canvas(dstBitmap);
        Canvas srcCanvas = new Canvas(srcBitmap);

        Paint dstPaint = new Paint();
        dstPaint.setColor(getResources().getColor(R.color.yellow));
        dstCanvas.drawCircle(80, 80, 80, dstPaint);

        Paint srcPaint = new Paint();
        srcPaint.setColor(getResources().getColor(R.color.blue));
        srcCanvas.drawRect(0, 0, 160, 160, srcPaint);

        //---第一排
        canvas.save();

        Bitmap b1 = Bitmap.createBitmap(240, 240, Bitmap.Config.ARGB_8888);
        Canvas c1 = new Canvas(b1);
        Paint p1 = new Paint();
        //定义运算模式
        p1.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        //创建图层
        int layer = canvas.saveLayer(new RectF(0, 0, 240, 240), null);
        //绘制底图
        c1.drawBitmap(dstBitmap, 0, 0, null);
        //绘制上层图
        c1.drawBitmap(srcBitmap, 80, 80, p1);
        //取消运算模式
        p1.setXfermode(null);
        //恢复
        canvas.restoreToCount(layer);
        canvas.drawBitmap(b1, 0, 0, p1);

        canvas.translate(250, 0);
        Bitmap b2 = Bitmap.createBitmap(240, 240, Bitmap.Config.ARGB_8888);
        Canvas c2 = new Canvas(b2);
        Paint p2 = new Paint();
        //定义运算模式
        p2.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
        //创建图层
        layer = canvas.saveLayer(new RectF(0, 0, 240, 240), null);
        //绘制底图
        c2.drawBitmap(dstBitmap, 0, 0, null);
        //绘制上层图
        c2.drawBitmap(srcBitmap, 80, 80, p2);
        //取消运算模式
        p2.setXfermode(null);
        //恢复
        canvas.restoreToCount(layer);
        canvas.drawBitmap(b2, 0, 0, p2);


        canvas.translate(250, 0);
        Bitmap b3 = Bitmap.createBitmap(240, 240, Bitmap.Config.ARGB_8888);
        Canvas c3 = new Canvas(b3);
        Paint p3 = new Paint();
        //定义运算模式
        p3.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST));
        //创建图层
        layer = canvas.saveLayer(new RectF(0, 0, 240, 240), null);
        //绘制底图
        c3.drawBitmap(dstBitmap, 0, 0, null);
        //绘制上层图
        c3.drawBitmap(srcBitmap, 80, 80, p3);
        //取消运算模式
        p3.setXfermode(null);
        //恢复
        canvas.restoreToCount(layer);
        canvas.drawBitmap(b3, 0, 0, p3);


        canvas.translate(250, 0);
        Bitmap b4 = Bitmap.createBitmap(240, 240, Bitmap.Config.ARGB_8888);
        Canvas c4 = new Canvas(b4);
        Paint p4 = new Paint();
        //定义运算模式
        p4.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
        //创建图层
        layer = canvas.saveLayer(new RectF(0, 0, 240, 240), null);
        //绘制底图
        c4.drawBitmap(dstBitmap, 0, 0, null);
        //绘制上层图
        c4.drawBitmap(srcBitmap, 80, 80, p4);
        //取消运算模式
        p4.setXfermode(null);
        //恢复
        canvas.restoreToCount(layer);
        canvas.drawBitmap(b4, 0, 0, p4);

        //---第一排结尾
        canvas.restore();


        //---第二排
        canvas.save();
        canvas.translate(0, 300);

        Bitmap b21 = Bitmap.createBitmap(240, 240, Bitmap.Config.ARGB_8888);
        Canvas c21 = new Canvas(b21);
        Paint p21 = new Paint();
        //定义运算模式
        p21.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER));
        //创建图层
        layer = canvas.saveLayer(new RectF(0, 0, 240, 240), null);
        //绘制底图
        c21.drawBitmap(dstBitmap, 0, 0, null);
        //绘制上层图
        c21.drawBitmap(srcBitmap, 80, 80, p21);
        //取消运算模式
        p21.setXfermode(null);
        //恢复
        canvas.restoreToCount(layer);
        canvas.drawBitmap(b21, 0, 0, p21);

        canvas.translate(250, 0);
        Bitmap b22 = Bitmap.createBitmap(240, 240, Bitmap.Config.ARGB_8888);
        Canvas c22 = new Canvas(b22);
        Paint p22 = new Paint();
        //定义运算模式
        p22.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        //创建图层
        layer = canvas.saveLayer(new RectF(0, 0, 240, 240), null);
        //绘制底图
        c22.drawBitmap(dstBitmap, 0, 0, null);
        //绘制上层图
        c22.drawBitmap(srcBitmap, 80, 80, p22);
        //取消运算模式
        p22.setXfermode(null);
        //恢复
        canvas.restoreToCount(layer);
        canvas.drawBitmap(b22, 0, 0, p22);


        canvas.translate(250, 0);
        Bitmap b23 = Bitmap.createBitmap(240, 240, Bitmap.Config.ARGB_8888);
        Canvas c23 = new Canvas(b23);
        Paint p23 = new Paint();
        //定义运算模式
        p23.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        //创建图层
        layer = canvas.saveLayer(new RectF(0, 0, 240, 240), null);
        //绘制底图
        c23.drawBitmap(dstBitmap, 0, 0, null);
        //绘制上层图
        c23.drawBitmap(srcBitmap, 80, 80, p23);
        //取消运算模式
        p23.setXfermode(null);
        //恢复
        canvas.restoreToCount(layer);
        canvas.drawBitmap(b23, 0, 0, p23);


        canvas.translate(250, 0);
        Bitmap b24 = Bitmap.createBitmap(240, 240, Bitmap.Config.ARGB_8888);
        Canvas c24 = new Canvas(b24);
        Paint p24 = new Paint();
        //定义运算模式
        p24.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
        //创建图层
        layer = canvas.saveLayer(new RectF(0, 0, 240, 240), null);
        //绘制底图
        c24.drawBitmap(dstBitmap, 0, 0, null);
        //绘制上层图
        c24.drawBitmap(srcBitmap, 80, 80, p24);
        //取消运算模式
        p24.setXfermode(null);
        //恢复
        canvas.restoreToCount(layer);
        canvas.drawBitmap(b24, 0, 0, p24);

        //---第二排结尾
        canvas.restore();

        //---第三排
        canvas.save();
        canvas.translate(0, 600);

        Bitmap b31 = Bitmap.createBitmap(240, 240, Bitmap.Config.ARGB_8888);
        Canvas c31 = new Canvas(b31);
        Paint p31 = new Paint();
        //定义运算模式
        p31.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
        //创建图层
        layer = canvas.saveLayer(new RectF(0, 0, 240, 240), null);
        //绘制底图
        c31.drawBitmap(dstBitmap, 0, 0, null);
        //绘制上层图
        c31.drawBitmap(srcBitmap, 80, 80, p31);
        //取消运算模式
        p31.setXfermode(null);
        //恢复
        canvas.restoreToCount(layer);
        canvas.drawBitmap(b31, 0, 0, p31);

        canvas.translate(250, 0);
        Bitmap b32 = Bitmap.createBitmap(240, 240, Bitmap.Config.ARGB_8888);
        Canvas c32 = new Canvas(b32);
        Paint p32 = new Paint();
        //定义运算模式
        p32.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
        //创建图层
        layer = canvas.saveLayer(new RectF(0, 0, 240, 240), null);
        //绘制底图
        c32.drawBitmap(dstBitmap, 0, 0, null);
        //绘制上层图
        c32.drawBitmap(srcBitmap, 80, 80, p32);
        //取消运算模式
        p32.setXfermode(null);
        //恢复
        canvas.restoreToCount(layer);
        canvas.drawBitmap(b32, 0, 0, p32);


        canvas.translate(250, 0);
        Bitmap b33 = Bitmap.createBitmap(240, 240, Bitmap.Config.ARGB_8888);
        Canvas c33 = new Canvas(b33);
        Paint p33 = new Paint();
        //定义运算模式
        p33.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP));
        //创建图层
        layer = canvas.saveLayer(new RectF(0, 0, 240, 240), null);
        //绘制底图
        c33.drawBitmap(dstBitmap, 0, 0, null);
        //绘制上层图
        c33.drawBitmap(srcBitmap, 80, 80, p33);
        //取消运算模式
        p33.setXfermode(null);
        //恢复
        canvas.restoreToCount(layer);
        canvas.drawBitmap(b33, 0, 0, p33);


        canvas.translate(250, 0);
        Bitmap b34 = Bitmap.createBitmap(240, 240, Bitmap.Config.ARGB_8888);
        Canvas c34 = new Canvas(b34);
        Paint p34 = new Paint();
        //定义运算模式
        p34.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR));
        //创建图层
        layer = canvas.saveLayer(new RectF(0, 0, 240, 240), null);
        //绘制底图
        c34.drawBitmap(dstBitmap, 0, 0, null);
        //绘制上层图
        c34.drawBitmap(srcBitmap, 80, 80, p34);
        //取消运算模式
        p34.setXfermode(null);
        //恢复
        canvas.restoreToCount(layer);
        canvas.drawBitmap(b34, 0, 0, p34);

        //---第三排结尾
        canvas.restore();
        //---第三排
        canvas.save();
        canvas.translate(0, 900);

        Bitmap b41 = Bitmap.createBitmap(240, 240, Bitmap.Config.ARGB_8888);
        Canvas c41 = new Canvas(b41);
        Paint p41 = new Paint();
        //定义运算模式
        p41.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DARKEN));
        //创建图层
        layer = canvas.saveLayer(new RectF(0, 0, 240, 240), null);
        //绘制底图
        c41.drawBitmap(dstBitmap, 0, 0, null);
        //绘制上层图
        c41.drawBitmap(srcBitmap, 80, 80, p41);
        //取消运算模式
        p41.setXfermode(null);
        //恢复
        canvas.restoreToCount(layer);
        canvas.drawBitmap(b41, 0, 0, p41);

        canvas.translate(250, 0);
        Bitmap b42 = Bitmap.createBitmap(240, 240, Bitmap.Config.ARGB_8888);
        Canvas c42 = new Canvas(b42);
        Paint p42 = new Paint();
        //定义运算模式
        p42.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.LIGHTEN));
        //创建图层
        layer = canvas.saveLayer(new RectF(0, 0, 240, 240), null);
        //绘制底图
        c42.drawBitmap(dstBitmap, 0, 0, null);
        //绘制上层图
        c42.drawBitmap(srcBitmap, 80, 80, p42);
        //取消运算模式
        p42.setXfermode(null);
        //恢复
        canvas.restoreToCount(layer);
        canvas.drawBitmap(b42, 0, 0, p42);


        canvas.translate(250, 0);
        Bitmap b43 = Bitmap.createBitmap(240, 240, Bitmap.Config.ARGB_8888);
        Canvas c43 = new Canvas(b43);
        Paint p43 = new Paint();
        //定义运算模式
        p43.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
        //创建图层
        layer = canvas.saveLayer(new RectF(0, 0, 240, 240), null);
        //绘制底图
        c43.drawBitmap(dstBitmap, 0, 0, null);
        //绘制上层图
        c43.drawBitmap(srcBitmap, 80, 80, p43);
        //取消运算模式
        p43.setXfermode(null);
        //恢复
        canvas.restoreToCount(layer);
        canvas.drawBitmap(b43, 0, 0, p43);


        canvas.translate(250, 0);
        Bitmap b44 = Bitmap.createBitmap(240, 240, Bitmap.Config.ARGB_8888);
        Canvas c44 = new Canvas(b44);
        Paint p44 = new Paint();
        //定义运算模式
        p44.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SCREEN));
        //创建图层
        layer = canvas.saveLayer(new RectF(0, 0, 240, 240), null);
        //绘制底图
        c44.drawBitmap(dstBitmap, 0, 0, null);
        //绘制上层图
        c44.drawBitmap(srcBitmap, 80, 80, p44);
        //取消运算模式
        p44.setXfermode(null);
        //恢复
        canvas.restoreToCount(layer);
        canvas.drawBitmap(b44, 0, 0, p44);

        //---第三排结尾
        canvas.restore();
    }
}
